package com.example.storage;

import static java.security.AccessController.getContext;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DBHelper extends SQLiteOpenHelper {
    private Context mcontext;
    public DBHelper(Context context,  String name,  SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mcontext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "create table user(" +
                "id integer primary key autoincrement," +
                " name varchar(40)," +
                " phone varchar(11))");
        Toast.makeText(mcontext, "创建成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insert(String string, String string1) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("insert into user(name,phone) values(?,?)", new String[]{string, string1});
    }

    public Boolean query(String string, String string1) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from user where name=? and phone=?", new String[]{string, string1});
        if (cursor.moveToFirst()) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(String string, String string1) {
        SQLiteDatabase db = getWritableDatabase();
        return db.delete("user", "name=? and phone=?", new String[]{string, string1}) > 0;
    }


//    public boolean update(String usernameText, String phoneText) {
//    // 根据手机号修改姓名
//        SQLiteDatabase db = getWritableDatabase();
//        return  db.update("user", new ContentValues(), "phone=?", new String[]{phoneText}) > 0;
//    }
}
